package com.my.blog.myblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.my.blog.annotation.SystemLog;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/14 14:29
 * @Description：
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminFileController {

    @PostMapping("/getFileList")
    public ResultBean<ArrayList<String>> getFileList(HttpServletRequest request){
        ResultBean<ArrayList<String>> resultBean=new ResultBean<>();
        String param="upload";
        ArrayList<String> list=new ArrayList<>();
        ArrayList<String> filenames = getFiles(param);
        //前面是这样的 http://localhost:82
        String parent=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/upload/";
        System.out.println("当前地址："+parent);
        if(filenames.size()>0){
            for(String filename:filenames){
                list.add(parent+filename);
            }
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setData(list);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 获取param文件夹下所有文件名
     * @param param
     * @return
     */
    public static ArrayList<String> getFiles(String param){
        File file=new File(param);
        //检索param文件夹下所有文件
        String[] list = file.list();
        ArrayList<String> filenameList=new ArrayList<>();
        if(list.length!=0){
            filenameList.addAll(Arrays.asList(list));
        }
        return filenameList;
    }
    public static void main(String[] args) {
        File file=new File("upload");
        if(!file.exists()){
            file.mkdir();
        }
        //检索param文件夹下所有文件
        String[] list = file.list();
        ArrayList<String> filenameList=new ArrayList<>();
        if(ObjectUtil.isNotEmpty(list)){
            filenameList.addAll(Arrays.asList(list));
        }
        System.out.println(filenameList);
    }

    @PostMapping("/deletePic")
    @SystemLog(description = ConstantUtil.DELETE_IMAGES,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deletePic(@RequestParam(value = "path")String path,HttpServletRequest request){
        ResultBean<Object> resultBean=new ResultBean<>();

        //用/分割
        String[] pathSplit = path.split("/");
        //System.out.println("用/分割:"+pathSplit);
        //取最后一个也就是文件名
        String filename = pathSplit[pathSplit.length - 1];
        System.out.println("文件名："+filename);
        //当前项目路径
        String property = System.getProperty("user.dir");
        //项目名
        String contextPath = request.getContextPath();
        System.out.println("项目名："+contextPath);
        //开始拼文件路径咯
        String filepath=property+"/"+contextPath+"/upload/"+filename;
        System.out.println("文件路径"+filepath);
        File file=new File(filepath);
        //文件存在
        if(file.exists()){
            //是个文件
            if(file.isFile()){
                //删除成功
                if(file.delete()){
                    resultBean.setCode(ResultBean.SUCCESS);
                    resultBean.setData("删除成功");
                }else {
                    resultBean.setCode(ResultBean.FAIL);
                    resultBean.setData("删除失败");
                }
            }else {
                resultBean.setCode(ResultBean.FAIL);
                resultBean.setData("出了点问题0.0");
            }
        }else {
            resultBean.setCode(ResultBean.FAIL);
            resultBean.setData("出了点问题0.0");
        }
        return resultBean;
    }
}
